package com.njit.office.utils;

import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.*;
import java.util.Date;

public class FileUtils {

    public static String getFileSuffix(File file){
        String fileName = file.getName();
        int index = fileName.lastIndexOf(".");
        return fileName.substring(index+1);
    }

    public static String getFileSuffix(String fileName){
        int index = fileName.lastIndexOf(".");
        return fileName.substring(index+1);
    }

    /**
     * 保存文件到指定路径
     * */
    public static boolean saveFile(File file,String savePath,String saveName){


        if (!file.exists()) {
            return false;
        }

        File path = new File(savePath);
        if (!path.exists()) {
            path.mkdirs();
        }
        File saveFile = new File(savePath + File.separator + saveName);


        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] bytes = new byte[1024];
        int len = 0;
        try {
            fis = new FileInputStream(file);
            fos = new FileOutputStream(saveFile);

            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            fos.flush();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    public static boolean saveFile(InputStream inputStream,String savePath,String saveName){



        File path = new File(savePath);
        if (!path.exists()) {
            path.mkdirs();
        }
        File saveFile = new File(savePath + File.separator + saveName);


        FileOutputStream fos = null;
        byte[] bytes = new byte[1024];
        int len = 0;
        try {

            fos = new FileOutputStream(saveFile);

            while ((len = inputStream.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            fos.flush();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static ResponseEntity<FileSystemResource> exportToResponseEntity(File file) {
        if (file == null) {
            return null;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=" +file.getName());
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Last-Modified", new Date(file.lastModified()).toString());
        headers.add("ETag", String.valueOf(System.currentTimeMillis()));

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new FileSystemResource(file));
    }

}
